import { defineStore } from 'pinia'

export const useConfigStore = defineStore('config', {
  state: () => ({
    savedConfigs: []
  }),

  actions: {
    saveConfig(name, nodes, connections) {
      const config = {
        id: Date.now(),
        name,
        date: new Date().toISOString(),
        nodes: JSON.parse(JSON.stringify(nodes)),
        connections: JSON.parse(JSON.stringify(connections))
      }
      
      this.savedConfigs.push(config)
      
      // 保存到本地存储
      localStorage.setItem('savedConfigs', JSON.stringify(this.savedConfigs))
    },

    loadConfig(id) {
      return this.savedConfigs.find(config => config.id === id)
    },

    deleteConfig(id) {
      const index = this.savedConfigs.findIndex(config => config.id === id)
      if (index !== -1) {
        this.savedConfigs.splice(index, 1)
        localStorage.setItem('savedConfigs', JSON.stringify(this.savedConfigs))
      }
    },

    loadSavedConfigs() {
      const saved = localStorage.getItem('savedConfigs')
      if (saved) {
        this.savedConfigs = JSON.parse(saved)
      }
    }
  }
}) 